﻿package uc{

	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.display.Stage;
	import flash.geom.Rectangle;
	import flash.geom.Point;
	import gs.TweenLite;

	public class Scroller extends Sprite {

		private static  var par:Object;
		public static  var content:MovieClip;
		private static  var scr:Object = new Object();
		private static  var st:Stage;

		public function Scroller() {
		}
		public static function setTarget(target:MovieClip, pars:Object=null) {
			st = target.stage;
			!pars ? pars = {} : null;
			
			uint(!pars.targetWidth) ? pars.targetWidth = target.width/2 : null;
			uint(!pars.targetHeight) ? pars.targetHeight = target.height/2 : null;

			uint(!pars.scrWidth) ? pars.scrWidth = 3 : null;
			uint(!pars.scrHeight) ? pars.scrHeight = pars.targetHeight : null;

			uint(!pars.scrColor) ? pars.scrColor = 0x333333 : null;
			uint(!pars.bgColor) ? pars.bgColor = 0xCCCCCC : null;

			uint(!pars.margin) ? pars.margin = 5 : null;

			!pars.align ? pars.align = "right" : null;

			Boolean(!pars.resize) ? pars.resize = true : null;

			uint(!pars.resWidth) || pars.resWidth<pars.scrWidth ? pars.resWidth = pars.scrWidth*2 : null;
			
			uint(!pars.area) ? pars.area = 10 : null;
			
			uint(!pars.wheelSpeed) ? pars.wheelSpeed = 10 : null;

			par = pars;
			content = target as MovieClip;
			
			scr.tarCont = new Sprite();
			content.addChild(scr.tarCont);
			
			var ilosc:uint = content.numChildren-1;
			for (var i:uint; i<ilosc; i++) {
				scr.tarCont.addChild(content.getChildAt(0));
			}
			
			initScroller();
		}
		private static function initScroller() {
			scr.scrCont = new Sprite();
			
			scr.scrBtn = new Sprite();
			scr.scrBtn.graphics.beginFill(par.scrColor);
			scr.scrBtn.graphics.drawRect(0, 0, par.scrWidth, 10);
			scr.scrBtn.name = "scrBtn";

			scr.scrTrk = new Sprite();
			scr.scrTrk.graphics.beginFill(par.bgColor);
			scr.scrTrk.graphics.drawRect(0, 0, par.scrWidth, par.scrHeight);

			scr.scrBg = new Sprite();
			scr.scrBg.graphics.beginFill(0, 0);
			scr.scrBg.graphics.drawRect(0, 0, par.scrWidth+2*par.area, par.scrHeight+2*par.area);
			scr.scrBg.x = scr.scrBg.y = -par.area;
			scr.scrBg.name = "scrBg";
			
			scr.tarMsk = new Sprite();
			scr.tarMsk.graphics.beginFill(0x009900);
			scr.tarMsk.graphics.drawRect(0, 0, par.targetWidth, par.targetHeight);
			
			scr.tarX = Math.round(scr.tarCont.getBounds(scr.tarCont).x);
			scr.tarY = Math.round(scr.tarCont.getBounds(scr.tarCont).y);
			scr.tarW = Math.round(scr.tarCont.getBounds(scr.tarCont).width);
			scr.tarH = Math.round(scr.tarCont.getBounds(scr.tarCont).height);
			
			scr.tarCont.x = -scr.tarX;
			scr.tarCont.y = -scr.tarY;
			
			if (par.align=="left") {
				scr.scrCont.x = - par.margin - par.scrWidth;
			} else {
				scr.scrCont.x = par.margin + par.targetWidth;
			}
			scr.tarCont.mask = scr.tarMsk;
			scr.scrBtn.visible = false;

			content.addChild(scr.tarMsk);
			scr.scrCont.addChild(scr.scrTrk);
			scr.scrCont.addChild(scr.scrBg);
			scr.scrCont.addChild(scr.scrBtn);

			content.addChild(scr.scrCont);

			scrUpd();
		}
		public static function scrUpd() {
			if (content.height>par.targetHeight) {
				scr.scrBtn.buttonMode = true;
				scr.scrBtn.height = par.scrHeight*(par.targetHeight/content.height);
				TweenLite.to(scr.scrBtn, .3, {autoAlpha:1});
				
				scr.scrCont.addEventListener(MouseEvent.MOUSE_OVER, szeroki);
				scr.scrCont.addEventListener(MouseEvent.MOUSE_OUT, nieSzeroki);
				scr.scrBtn.addEventListener(MouseEvent.MOUSE_DOWN, scrolluj);
				content.addEventListener(MouseEvent.MOUSE_WHEEL, scrollWheel);
				
				scr.scrMultiplier = (par.scrHeight-scr.scrBtn.height)/(par.scrHeight-scr.scrBtn.height);
				scr.tarMultiplier = -(content.height-par.targetHeight)/(par.scrHeight-scr.scrBtn.height);
			} else {
				scr.scrBtn.buttonMode = false;
				TweenLite.to(scr.scrBtn, .3, {autoAlpha:0, height:0});
				
				scr.scrCont.removeEventListener(MouseEvent.MOUSE_OVER, szeroki);
				scr.scrCont.removeEventListener(MouseEvent.MOUSE_OUT, nieSzeroki);
				scr.scrBtn.removeEventListener(MouseEvent.MOUSE_DOWN, scrolluj);
				scr.scrCont.removeEventListener(MouseEvent.MOUSE_WHEEL, scrollWheel);
				
				scr.scrMultiplier = null;
				scr.tarMultiplier = null;
			}
			scr.scrOff = 0;
		}
		private static function szeroki(e:MouseEvent) {
			st.removeEventListener(MouseEvent.MOUSE_UP, nieSzeroki);
			
			if (par.resize) {
				if (par.align!="left") {
					TweenLite.to(scr.scrBtn, .3, {width:par.resWidth});
					TweenLite.to(scr.scrTrk, .3, {width:par.resWidth});
				} else {
					TweenLite.to(scr.scrBtn, .3, {width:par.resWidth, x:par.scrWidth-par.resWidth});
					TweenLite.to(scr.scrTrk, .3, {width:par.resWidth, x:par.scrWidth-par.resWidth});
				}
			}
		}
		private static function nieSzeroki(e:MouseEvent) {
			st.removeEventListener(MouseEvent.MOUSE_UP, nieSzeroki);
			
			if(!e.buttonDown){
				if (par.align!="left") {
					TweenLite.to(scr.scrBtn, .3, {width:par.scrWidth});
					TweenLite.to(scr.scrTrk, .3, {width:par.scrWidth});
				} else {
					TweenLite.to(scr.scrBtn, .3, {width:par.scrWidth, x:0});
					TweenLite.to(scr.scrTrk, .3, {width:par.scrWidth, x:0});
				}
			}else{
				st.addEventListener(MouseEvent.MOUSE_UP, nieSzeroki);
			}
		}
		private static function scrolluj(e:MouseEvent) {
			scr.scrBtn.startDrag(false, new Rectangle(scr.scrTrk.x, scr.scrTrk.y, 0, scr.scrTrk.height-scr.scrBtn.height));
			st.addEventListener(MouseEvent.MOUSE_MOVE, przewin);
			st.addEventListener(MouseEvent.MOUSE_UP, nieScrolluj);
		}
		private static function nieScrolluj(e:MouseEvent) {
			scr.scrBtn.stopDrag();
			st.removeEventListener(MouseEvent.MOUSE_MOVE, przewin);
			st.removeEventListener(MouseEvent.MOUSE_UP, nieScrolluj);
		}
		private static function przewin(e:MouseEvent) {
			TweenLite.to(scr.tarCont, .3, {y:scr.scrBtn.y*scr.tarMultiplier});
			scr.scrOff = scr.scrBtn.y;
		}
		private static function scrollWheel(e:MouseEvent) {
			scr.scrOff -= e.delta*par.wheelSpeed*(scr.scrBtn.height/scr.scrTrk.height);
			if (scr.scrOff<0) {
				scr.scrOff = 0;
			} else if (scr.scrOff>par.scrHeight-scr.scrBtn.height) {
				scr.scrOff = par.scrHeight-scr.scrBtn.height;
			}
			TweenLite.to(scr.scrBtn, .3, {y:scr.scrOff*scr.scrMultiplier});
			TweenLite.to(scr.tarCont, .3, {y:scr.scrOff*scr.tarMultiplier});
		}
	}
}